package uit.shoeshop.model;

import uit.shoeshop.dao.CityDAO;
import uit.shoeshop.dto.BillDetailDTO;
import uit.shoeshop.dto.BillInfoDTO;
import uit.shoeshop.dto.CityDTO;
import uit.shoeshop.dto.ProductDTO;
import uit.shoeshop.dao.BillDAO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import uit.shoeshop.model.CheckoutModel;
import java.sql.SQLException;

public class PaymentModel {
	public PaymentModel(){
		
	}
	
	public ArrayList<CityDTO> getAllCity() throws SQLException{
		ArrayList<CityDTO> city = new ArrayList<CityDTO>();

		CityDAO tblCity = new CityDAO();

		city = tblCity.getAllCity();
		
		tblCity = null;
		return city;		
	}
	
	public int insertBill(HashMap<Integer,Integer> cart,String company,String fullname,String address,String phone,String city,String comment ) throws SQLException{
		int result = 1;
		HashMap<Integer,ProductDTO> products;
		CheckoutModel checkout = new CheckoutModel();		
		products = checkout.getAllProductInCart(cart);

		boolean checkStock = true;
		ProductDTO product;
		Set set = cart.entrySet();
    	Iterator i = set.iterator();
    	while(i.hasNext()) {
        	Map.Entry me = (Map.Entry)i.next();
        	Integer key = Integer.parseInt(me.getKey().toString());
        	Integer value = Integer.parseInt(me.getValue().toString());
        	product = products.get(key);
        	if(product.getIn_stock() < value){
        		checkStock = false;
        		break;
        	}
    	}		
		
    	if(checkStock == false)
    		return 0;
    	
		BillDAO bill = new BillDAO();
		result = bill.insertBill(cart,products,company,fullname,address,phone,city,comment);
		
		bill = null;
		return result;
	}
	
	public ArrayList<BillInfoDTO> getAllBill(int page) throws SQLException{
		ArrayList<BillInfoDTO> billinfo = new ArrayList<BillInfoDTO>();

		BillDAO tblBillinfo = new BillDAO();
		/*
		 * Lay tat ca cac product cua category
		 */
		billinfo = tblBillinfo.getAllBill(page);
		
		tblBillinfo = null;
		return billinfo;
	}
	
	public int countTotalBill() throws SQLException{
		int result = 0;
		BillDAO tblBillinfo = new BillDAO();
		result = tblBillinfo.countTotalBill();
		return result;
	}
	
	public ArrayList<BillDetailDTO> getAllBillDetail(int sId) throws SQLException{
		ArrayList<BillDetailDTO> billdetail = new ArrayList<BillDetailDTO>();

		BillDAO tblBilldetail = new BillDAO();
		
		billdetail = tblBilldetail.getAllBillDetail(sId);
		
		tblBilldetail = null;
		return billdetail;
	}
	
	public int delBill(int sId) throws SQLException{
		int result = 1;
		BillDAO tblBillinfo = new BillDAO();
		result = tblBillinfo.delBill(sId);
		return result;
	}
	
	public int editBill(int sId,int accepted) throws SQLException{
		int result = 1;
		BillDAO tblBillinfo = new BillDAO();
		result = tblBillinfo.editBill(sId,accepted);
		return result;
	}
	
	public int getTotalPrice(ArrayList<BillDetailDTO> bills) throws SQLException{
		int result = 0;
		for(BillDetailDTO bill : bills){
			result+= bill.getPrice()*bill.getQty();
		}
		return result;
	}
	
	public BillInfoDTO getBillbyID(int sId) throws SQLException{
		BillInfoDTO billinfo = new BillInfoDTO();

		BillDAO tblBillinfo = new BillDAO();
		
		billinfo = tblBillinfo.getBillbyID(sId);
		
		tblBillinfo = null;
		return billinfo;
	}
}
